package org.slsale.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slsale.common.Constants;
import org.slsale.pojo.User;
import org.slsale.service.user.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

@Controller
public class LoginController{

	@Resource
	private UserService userService;

	@RequestMapping(value="/login.html")
	@ResponseBody
	public  Object login(@RequestParam String user,
			HttpSession session){
		if(null==user||"".equals(user)){
			return "nodata";
		}
		JSONObject userObject=JSONObject.fromObject(user);
		User userObj=(User) JSONObject.toBean(userObject, User.class);

		if(userService.loginCodeIsExit(userObj) ==  0){//不存在这个登陆账号
			return "nologincode";
		}
		
		User _user = userService.getLoginUser(userObj);
		if(null != _user){//登录成功
			session.setAttribute(Constants.SESSION_USER, _user);
			User updateLoginTimeUser = new User();
			updateLoginTimeUser.setId(_user.getId());
			updateLoginTimeUser.setLastLoginTime(new Date());
			userService.modifyUser(updateLoginTimeUser);//更新当前用户登录的lastLoginTime
			updateLoginTimeUser = null;
			return "success";
		} 
		return "pwderror"; //密码错误
	}

	@RequestMapping("/main.html")
	public String main(){
		return "main";
	}






}
